import request from '@/utils/request';

export type IProject = {
  project_id: number;
  project_name: string;
};

export type ITag = {
  id: number;
  tag_name: string;
};

export type IRca = {
  id?: number;
  key?: number;
  fault_id?: string;
  description?: string;
  level?: number;
  level_name?: string;
  create_user_name?: string;
  project_list?: IProject[];
  effect_project_list?: IProject[];
  tag_list?: ITag[];
  create_time?: string;
  duration_time?: string;
  discovery_time?: string;
};

export type IRcaQuery = {
  project_ids?: number[];
  fault_levels?: number[];
  tags?: number[];
  search_name?: string;
  sort_by_discovery_time?: string;
  start_date?: string;
  end_date?: string;
  page_number?: number;
  page_size?: number;
};

export async function createFault<T>(data: T): Promise<T> {
  return request('/v1/itsm/fault', {
    method: 'POST',
    data,
  });
}

export function updateFault<T>(fault_id: string | number, data: T): Promise<T> {
  return request(`/v1/itsm/fault/${fault_id}`, {
    method: 'PUT',
    data,
  });
}

export function getFaultList<T>(data: IRcaQuery): Promise<T> {
  return request('/v1/itsm/fault/list', {
    method: 'POST',
    data,
  });
}

export function getFaultDetail<T>(fault_id: string | number): Promise<T> {
  return request(`/v1/itsm/fault/${fault_id}`, {
    method: 'GET',
  });
}

export function deleteFault<T>(data: T): Promise<T> {
  return request('/v1/itsm/fault/delete', {
    method: 'POST',
    data,
  });
}

export async function getFaultLevel() {
  return request('/v1/itsm/fault_level');
}

export async function getFaultTag() {
  return request('/v1/itsm/fault_tag/list', {
    method: 'POST',
    data: {},
  });
}
